<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\model;

class Police extends \app\BaseModel
{
    protected $name = "massage_service_coach_police";
    public function dataAdd($data)
    {
        $data["create_time"] = time();
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page = 10)
    {
        $data = $this->alias("a")->join("massage_service_coach_list b", "b.id = a.coach_id")->where($dis)->field("a.*,b.coach_name,b.mobile")->group("a.id")->order("a.id desc")->paginate($page)->toArray();
        return $data;
    }
    public function noLogindataList($dis, $page = 10)
    {
        $data = $this->alias("a")->join("massage_service_coach_list b", "b.id = a.coach_id")->where($dis)->field("a.*,b.coach_name")->group("a.id")->order("a.id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function helpSendMsgWechat($uniacid, $coach_id, $address, $openid)
    {
        $cap_model = new Coach();
        $config_model = new SendMsgConfig();
        $config_model->initData($uniacid);
        $x_config = $config_model->dataInfo(["uniacid" => $uniacid]);
        $config = longbingGetAppConfig($uniacid);
        if(empty($x_config["gzh_appid"]) || empty($x_config["help_tmpl_id"])) {
            return false;
        }
        $coach_name = $cap_model->where(["id" => $coach_id])->value("coach_name");
        $access_token = longbingGetAccessToken($uniacid);
        $page = "/pages/index";
        $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=" . $access_token;
        $data = ["touser" => $openid, "mp_template_msg" => ["appid" => $x_config["gzh_appid"], "url" => "http://weixin.qq.com/download", "template_id" => $x_config["help_tmpl_id"], "miniprogram" => ["appid" => $config["appid"], "page" => $page], "data" => ["keyword1" => ["value" => $coach_name, "color" => "#93c47d"], "keyword2" => ["value" => $address, "color" => "#0000ff"], "keyword3" => ["value" => date("Y-m-d H:i", time()), "color" => "#0000ff"]]]];
        $data = json_encode($data);
        $tmp = ["url" => $url, "data" => $data];
        $rest = lbCurlPost($tmp["url"], $tmp["data"]);
        $rest = json_decode($rest, true);
        return $rest;
    }
    public function helpSendMsgWeb($uniacid, $coach_id, $address, $openid)
    {
        $cap_model = new Coach();
        $config_model = new SendMsgConfig();
        $config_model->initData($uniacid);
        $x_config = $config_model->dataInfo(["uniacid" => $uniacid]);
        if(empty($x_config["gzh_appid"]) || empty($x_config["help_tmpl_id"])) {
            return false;
        }
        $coach_name = $cap_model->where(["id" => $coach_id])->value("coach_name");
        $wx_setting = new \longbingcore\wxcore\WxSetting($uniacid);
        $access_token = $wx_setting->getGzhToken();
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
        $key = explode("&", $x_config["help_tmpl_id"]);
        for ($i = 1; $i < 4; $i++) {
            $arr[$i] = !empty($key[$i]) ? $key[$i] : "keyword" . $i;
        }
        $data = ["touser" => $openid, "appid" => $x_config["gzh_appid"], "url" => "https://" . $_SERVER["HTTP_HOST"] . "/h5/", "template_id" => $key[0], "data" => [$arr[1] => ["value" => $coach_name, "color" => "#93c47d"], $arr[2] => ["value" => $arr[2] == "keyword2" ? $address : mb_substr($address, 0, 20), "color" => "#93c47d"], $arr[3] => ["value" => date("Y-m-d H:i", time()), "color" => "#93c47d"]]];
        $data = json_encode($data);
        $tmp = ["url" => $url, "data" => $data];
        $rest = lbCurlPost($tmp["url"], $tmp["data"]);
        $rest = json_decode($rest, true);
        return $rest;
    }
    public function helpSendMsg($uniacid, $coach_id, $address)
    {
        $help_model = new HelpConfig();
        $coach_model = new Coach();
        $user_model = new User();
        $res = true;
        $help_config = $help_model->dataInfo(["uniacid" => $uniacid]);
        $coach_info = $coach_model->dataInfo(["id" => $coach_id]);
        if($help_config["tmpl_admin_status"] == 1 && !empty($help_config["help_user_id"]) && (empty($coach_info["admin_id"]) || $help_config["tmpl_notice_admin"] == 1)) {
            $user_list = $user_model->where("id", "in", $help_config["help_user_id"])->select()->toArray();
            if(empty($user_list)) {
                return false;
            }
            foreach ($user_list as $user_info) {
                $type = $user_info["last_login_type"] == 0 && !empty($user_info["wechat_openid"]) ? 1 : 2;
                $res = $this->helpSendMsgWeb($uniacid, $coach_id, $address, $user_info["web_openid"]);
            }
        }
        if(!empty($coach_info["admin_id"]) && $help_config["help_wechat_agent_status"]) {
            $admin_model = new Admin();
            $user_id = $admin_model->where(["id" => $coach_info["admin_id"]])->value("user_id");
            $user_info = $user_model->dataInfo(["id" => $user_id]);
            if(!empty($user_info)) {
                $type = $user_info["last_login_type"] == 0 && !empty($user_info["wechat_openid"]) ? 1 : 2;
                $res = $this->helpSendMsgWeb($uniacid, $coach_id, $address, $user_info["web_openid"]);
            }
        }
        return $res;
    }
    public function sendPoliceNotice($uniacid, $coach_id, $address)
    {
        $config_model = new ShortCodeConfig();
        $reminder_model = new \app\reminder\model\Config();
        $config_model->sendHelpCode($uniacid, $coach_id, $address);
        $this->helpSendMsg($uniacid, $coach_id, $address);
        $coach_model = new Coach();
        $caoch_info = $coach_model->dataInfo(["id" => $coach_id]);
        $reminder_model->sendCalledPolice($caoch_info);
        return true;
    }
}

?>